package com.ruoyi.tongban.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tongban.domain.InsClassCamera;
import com.ruoyi.tongban.domain.TInstitution;
import com.ruoyi.tongban.mapper.InsClassCameraMapper;
import com.ruoyi.tongban.service.IInsClassCameraService;
import com.ruoyi.tongban.service.ITInstitutionService;
import com.ruoyi.tongban.utils.PinYinUtils;
import com.ruoyi.tongban.utils.PushCameraUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tongban.mapper.InsCameraMapper;
import com.ruoyi.tongban.domain.InsCamera;
import com.ruoyi.tongban.service.IInsCameraService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 视频监控Service业务层处理
 * 
 * @author caiyanshan
 * @date 2021-03-09
 */
@Service
public class InsCameraServiceImpl implements IInsCameraService 
{
    @Autowired
    private InsCameraMapper insCameraMapper;
    
    @Autowired
    private ITInstitutionService institutionService;//机构
    
    @Autowired//班级 摄像头中间表
    private IInsClassCameraService insClassCameraService;

    /**
     * 查询视频监控
     * 
     * @param cameraId 视频监控ID
     * @return 视频监控
     */
    @Override
    public InsCamera selectInsCameraById(Long cameraId)
    {
        return insCameraMapper.selectInsCameraById(cameraId);
    }

    /**
     * 查询视频监控列表
     * 
     * @param insCamera 视频监控
     * @return 视频监控
     */
    @Override
    public List<InsCamera> selectInsCameraList(InsCamera insCamera)
    {
        return insCameraMapper.selectInsCameraList(insCamera);
    }

    /**
     * 新增视频监控
     * 
     * @param insCamera 视频监控
     * @return 结果
     */
    @Override
    @Transactional
    public int insertInsCamera(InsCamera insCamera)
    {
        this.handlePushUrl(insCamera);
    
        insCameraMapper.insertInsCamera(insCamera);
        
        InsClassCamera classCamera = new InsClassCamera();//中间表数据插入
        classCamera.setCameraId(insCamera.getCameraId());
        classCamera.setClassId(insCamera.getClassId());
        classCamera.setCreateTime(DateUtils.getNowDate());
        insClassCameraService.insertInsClassCamera(classCamera);
        return 1;
    }
    
    /**
     * @Description 重新生成推流地址
     * @Author caiyanshan
     * @Date 2021/3/9 16:03
     * @Param [cameraId]
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    public AjaxResult againCreatePushUrl(Long cameraId, String loseTime){
        InsCamera insCamera = insCameraMapper.selectInsCameraById(cameraId);
        insCamera.setLoseTime(loseTime);
        this.handlePushUrl(insCamera);
        return AjaxResult.success(insCameraMapper.updateInsCamera(insCamera));
    }
    
    /**
     * @Description 处理推流地址数据
     * @Author caiyanshan
     * @Date 2021/3/9 16:05
     * @Param []
     * @Return java.util.Map<java.lang.String,java.lang.String>
     */
    public void handlePushUrl(InsCamera insCamera){
    
        TInstitution institution = institutionService.selectTInstitutionById(insCamera.getInstitutionId());
        Map<String,String> returnMap = PushCameraUtils.getPushUrl(PinYinUtils.getPinyin(institution.getInstitutionName()),
                insCamera.getLoseTime());
        insCamera.setPushStream(returnMap.get("pushStream"));
        insCamera.setCameraHttp(returnMap.get("cameraRtmp"));
        insCamera.setCameraRtmp(returnMap.get("cameraHttp"));
        insCamera.setCameraManufacturer("本心科技");
        insCamera.setCameraBrand("本心科技");
        insCamera.setCameraModel("本心科技");
    }

    /**
     * 修改视频监控
     * 
     * @param insCamera 视频监控
     * @return 结果
     */
    @Override
    public int updateInsCamera(InsCamera insCamera)
    {
        return insCameraMapper.updateInsCamera(insCamera);
    }

    /**
     * 批量删除视频监控
     * 
     * @param cameraIds 需要删除的视频监控ID
     * @return 结果
     */
    @Override
    public int deleteInsCameraByIds(Long[] cameraIds)
    {
        return insCameraMapper.deleteInsCameraByIds(cameraIds);
    }

    /**
     * 删除视频监控信息
     * 
     * @param cameraId 视频监控ID
     * @return 结果
     */
    @Override
    public int deleteInsCameraById(Long cameraId)
    {
        return insCameraMapper.deleteInsCameraById(cameraId);
    }
}
